package ohc.login.mixin;

import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import ohc.login.LoginManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

/**
 * 阻止未登录玩家移动
 */
@Mixin(ServerPlayNetworkHandler.class)
public class PlayerMovementMixin {
    
    @Shadow
    public ServerPlayerEntity player;
    
    // 上次传送时间
    private long lastTeleportTime = 0;
    private static final long TELEPORT_COOLDOWN_MS = 10; // 
    
    @Inject(method = "onPlayerMove", at = @At("HEAD"), cancellable = true)
    private void onPlayerMove(PlayerMoveC2SPacket packet, CallbackInfo ci) {
        if (!LoginManager.INSTANCE.isLoggedIn(player.getUuid())) {
            long now = System.currentTimeMillis();
            if (now - lastTeleportTime > TELEPORT_COOLDOWN_MS) {
                lastTeleportTime = now;
                player.networkHandler.requestTeleport(
                    player.getX(),
                    player.getY(),
                    player.getZ(),
                    player.getYaw(),
                    player.getPitch()
                );
            }
            ci.cancel();
        }
    }
}
